<template>
  <div 
    class="message-item"
    :class="{ 'message-mine': isMine }"
  >
    <div v-if="!isMine" class="avatar">
      <img 
        :src="sender.avatar" 
        :alt="sender.name"
        @error="handleImageError"
        @load="handleImageLoad"
        :class="{ 'loading': isLoading }"
      >
    </div>
    <div class="message-content">
      <p class="sender">{{ sender.name }}</p>
      <div class="bubble">{{ message.content }}</div>
      <span class="time">{{ formatTimeString }}</span>
    </div>
    <div v-if="isMine" class="avatar">
      <img 
        :src="sender.avatar" 
        :alt="sender.name"
        @error="handleImageError"
        @load="handleImageLoad"
        :class="{ 'loading': isLoading }"
      >
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, ref } from 'vue'
import type { Message, User } from '../types'
import { useChatStore } from '../stores/chat'
import { formatTime } from '../utils/time'
import { DEFAULT_AVATAR } from '../constants/default'

const props = defineProps<{
  message: Message
  sender: User
}>()

const store = useChatStore()
const isMine = computed(() => props.message.sender === store.currentUser.id)

const formatTimeString = computed(() => {
  return formatTime(props.message.timestamp)
})

const isLoading = ref(true)

const handleImageError = (e: Event) => {
  const target = e.target as HTMLImageElement
  // 根据用户类型设置不同的默认头像
  target.src = isMine.value ? DEFAULT_AVATAR.USER : DEFAULT_AVATAR.BOT
}

const handleImageLoad = () => {
  isLoading.value = false
}
</script>

<style scoped>
.message-item {
  display: flex;
  margin: 1rem 0;
  padding: 0 1rem;
  align-items: flex-start;
}

.message-mine {
  justify-content: flex-end;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  overflow: hidden;
  margin: 0 8px;
  flex-shrink: 0;
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: opacity 0.3s;
}

.avatar img.loading {
  opacity: 0.6;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0% { opacity: 0.6; }
  50% { opacity: 0.3; }
  100% { opacity: 0.6; }
}

.message-content {
  max-width: calc(70% - 96px);
}

.sender {
  margin: 0;
  font-size: 0.8rem;
  color: #666;
}

.bubble {
  background: #e9ecef;
  padding: 0.5rem 1rem;
  border-radius: 1rem;
  margin: 0.2rem 0;
  word-break: break-word;
}

.message-mine .bubble {
  background: #007bff;
  color: white;
}

.time {
  font-size: 0.7rem;
  color: #999;
  margin-top: 0.2rem;
  display: block;
}

/* 添加气泡尾巴效果 */
.bubble {
  position: relative;
}

.message-mine .bubble::after {
  content: '';
  position: absolute;
  right: -6px;
  top: 50%;
  transform: translateY(-50%);
  border-left: 6px solid #007bff;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
}

.bubble::before {
  content: '';
  position: absolute;
  left: -6px;
  top: 50%;
  transform: translateY(-50%);
  border-right: 6px solid #e9ecef;
  border-top: 6px solid transparent;
  border-bottom: 6px solid transparent;
}

.message-mine .bubble::before {
  display: none;
}

.message-item:not(.message-mine) .bubble::after {
  display: none;
}

/* 添加头像悬停效果 */
.avatar img:hover {
  transform: scale(1.1);
  transition: transform 0.2s ease;
}

.message-mine .avatar {
  border-color: #007bff;
}
</style> 